package me.xiaoyan.android.tuition.activity;


import me.xiaoyan.android.tuition.R;
import me.xiaoyan.android.tuition.utils.AppManager;
import me.xiaoyan.android.widget.YLog;
import me.xiaoyan.json.JSONArray;
import me.xiaoyan.json.JSONException;
import me.xiaoyan.json.JSONObject;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class PayItems extends Common {
	private static final String TAG = "Activity.PayItems";

	private JSONArray dataArr = new JSONArray();
	private ListView payItemList = null;
	private Context context;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_payitems);
		context = this;
		setPageTitle(getString(R.string.header_select_topay));
		payItemList = (ListView) findViewById(R.id.lv_payitems);
		if (AppManager.getInstance().hasAppDatas(STUDENTS_PAYS)) {
			dataArr = (JSONArray) AppManager.getInstance().getAppDatas(
					STUDENTS_PAYS);
			if (dataArr == null || dataArr.length() < 1) {
				super.goback();
				return;
			}
		}
		payItemList.setAdapter(new PayItemAdapter());
		super.showBackButton();
	}

	private class PayItemAdapter extends BaseAdapter {
		private LayoutInflater inflater;

		public PayItemAdapter() {
			inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return dataArr.length();
		}

		@Override
		public Object getItem(int arg0) {
			return arg0;
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int position, View v, ViewGroup arg2) {
			v = inflater.inflate(R.layout.ui_pay_item, null);
			if (position % 2 == 0) {
				v.setBackgroundColor(Color.rgb(255, 255, 255));
			} else {
				v.setBackgroundColor(Color.rgb(245, 245, 245));
			}
			String itemName = "";
			try {
				final JSONObject orderinfo = dataArr.getJSONObject(position);
				itemName = orderinfo.optString("tuitionItemName");
				v.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						AppManager.getInstance().saveAppDatas(PAY_INFO,
								orderinfo);
						startActivity(new Intent(PayItems.this, OrderInfo.class));
					}
				});
			} catch (JSONException e) {
				YLog.e(TAG, e);
			}
			((TextView) v.findViewById(R.id.txt_ui_payitem)).setText(itemName);
			return v;
		}

	}

	@Override
	public void onBackPressed() {
		goback();
	}
}
